package com.itheima.controller;

import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.resource.MessageConstant;
import com.itheima.resource.RedisMessageConstant;
import com.itheima.service.OrderService;
import io.netty.handler.codec.redis.RedisMessage;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RequestMapping("/order")
@RestController
public class OrderController {

    @Reference
    private OrderService orderService;

    @Autowired
    private JedisPool jedisPool;
    String key = null;

    /**
     * 从redis中获取存在的验证码，判断验证码是否正确
     *
     * @return
     */
    @RequestMapping("/validateCode.do")
    public Result validateCode(@RequestParam("code") Integer code, @RequestParam("telephone") String telephone) {
        key = telephone + RedisMessageConstant.SENDTYPE_ORDER;
        String redisCode = jedisPool.getResource().get(key);
        if (redisCode != null && redisCode.equals(code.toString())) {//验证码正确
            return new Result(true, null);
        } else {
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }

    }

    /**
     * 添加预约套餐
     *
     * @return
     */
    @RequestMapping("/addOrder.do")
    public Result addOrder(@RequestBody Map<String, Object> map) {
        Jedis jedis = jedisPool.getResource();
        try {
            //如果用户在填写完信息后没有立即提交表单，验证码有可能会超时，要先判断发送的表单中的
            //验证码是否已经超时
            String redisCode = jedis.get(key);
            jedis.close();
            if (redisCode == null) { //验证码已过期
                return new Result(false, MessageConstant.VALIDATECODE_OUTTIME);
            } else { //验证码未过期,判断验证码是否正确
                String code = (String) map.get("validateCode");
                if (redisCode.equals(code)) {//验证码正确,调用service层添加预约套餐
                    Order order = orderService.addOrder(map);
                    //重定向到支付模块
                    //goToPay(order.getId()+"",1+"");


                    return new Result(true, MessageConstant.ADD_ORDER_SUCCESS, order);
                } else {
                    return new Result(false, MessageConstant.VALIDATECODE_ERROR);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }
    }


    @RequestMapping("/findOrderDetail.do")
    public Result findOrderDetail(@RequestParam("id") Integer id) {

        try {
            Map map = orderService.findOrderDetail(id);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_ORDER_FAIL);
        }


    }


    //查询订单状态

    @RequestMapping("/findState.do")
    public Result findState(@RequestParam("oid") Integer oid) {

        Order order = orderService.findStatus(oid);
        if (order.getPayStatus() == 0) {
            return new Result(false, "未支付");// 未支付
        } else {
            return new Result(true, "已支付"); // 已支付
        }

    }
}